<?php
class Login extends MY_Controller {
	var $caption = "";
	var $currentPage = 'login';
	var $action = '';
	
		public function index(){
			$this->createAdminAccount();
			
			$data = '';
		
			$username = $this->input->post('username',TRUE);
			$password = $this->input->post('password',TRUE);
			
			if(isset($username)&&isset($password)){
				$password  = crypt($password,PASSWORDSALT);
				//$checkUser = $this->db->query("select * from user where  username = '$username' and password = '$password' and (usertype='A' or usertype='S') limit 1");
				$checkUser = $this->db->query("select * from user where  username = '$username' and password = '$password' and activated = 1 limit 1");
			
				
				if($checkUser->row() != null){
					$this->session->set_userdata('user_id', $checkUser->row()->id);
					$this->session->set_userdata('user_fullname', $checkUser->row()->firstname." ".$checkUser->row()->middlename." ".$checkUser->row()->lastname);
					$this->session->set_userdata('user_email', $checkUser->row()->email);
					//$this->session->set_userdata('user_type', $checkUser->row()->usertype);
					$this->caption = "Dashboard";
					redirect(ADMINBASEURL.'/dashboard');
				}
				else{
				}
			}

			$this->loadAdminPage($data = '');
	}
	
	
	
	public function create(){
		$data = '';
		$this->action = 'add';
		$this->loadAdminPage($data);
	}

	public function logout(){

		$this->session->unset_userdata('user_id');
		$this->session->unset_userdata('user_fullname');
		$this->session->unset_userdata('user_email');
		$this->session->unset_userdata('user_type');
		$this->loadAdminPage($data = '');
		
	}
	
	private function createAdminAccount() {
		$user = $this->mUser;
		if(!$user->hasAdminAccount()) {
    		$user->username = 'admin';
    		$user->password = crypt('q',PASSWORDSALT);
    		$user->activated = 1;
    		$user->email = 'admin@webcreation.com';
    		$user->usertype = 'ADMIN';
    		$user->insert();
		}
	}
}